package cc.tuhaolicai.mt.user.mobile.validator;
import cc.tuhaolicai.mt.user.mobile.controller.BaseController;
import com.google.common.collect.Maps;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

import java.util.Map;

/**
 * 防止表单提交验证类
 */
public class ResubmitValidator extends Validator {

    private transient Map<String,String> errorMsg = Maps.newHashMap();

    //这是第一步
    @Override
    protected void validate(Controller controller){

        validateToken("resubmit","msg","重复提交了");
    }

    //这是第三步
    @Override
    protected void handleError(Controller controller) {
        BaseController baseController = (BaseController)controller;
        controller.renderJson(errorMsg);
        controller.setAttr("msg",errorMsg.get("msg"));
        baseController.render("/WEB-INF/view/message/error.jsp");
    }

    //这是第二步
    @Override
    protected void addError(String errorKey,String errorMessage){

        invalid = true;
        errorMsg.put(errorKey,errorMessage);
    }
}
